package com.common.config;


import com.common.utils.StringUtils;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;


@Configuration//配置类
@EnableSwagger2 //swagger注解
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){
        List<String> excludedPathPrefix = Arrays.asList(
                "/error",
                "/actuator"
        );
        return new Docket(DocumentationType.SWAGGER_2)
                //用于分组功能，也可以不配置
//                .groupName("admin")
                //注册整体api信息
                .apiInfo(webApiInfo())
                //swagger功能是否启用，可以通过配置设置，也可以先写死
//                .enable()
                .select()
                //不显示错误的接口地址,错误路径不监控
//                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .paths((s) -> {
                    for(String pathPrefix : excludedPathPrefix) {
                        if(StringUtils.startsWith(s, pathPrefix)) {
                            return false;
                        }
                    }
                    return true;
                })
                //指定扫描的包
//                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                //设置此组只匹配admin/**的请求
//                .paths(PathSelectors.ant("/admin/**"))
                .build();
    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder().title("后台管理项目")
                .description("通用的CRUD")
                .contact(new Contact("Van", "", ""))
                .version("1.0.0")
                .build();
    }
}
